package com.friddle.captch.FiveEightCaptch;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.NameValuePair;
import org.apache.http.cookie.Cookie;
import org.apache.http.message.BasicNameValuePair;

import com.friddle.Download.PageGet.PageGetter;
import com.friddle.Download.PageGet.PageResult;
import com.friddle.captch.Captch;
import com.friddle.ocr.OcrParse;
import com.friddle.ocr.UUOcr.UUConstant;
import com.friddle.util.Assert;
import com.friddle.util.Function.UrlFunction;
import com.friddle.util.Html.Html;

/**
 * Created by friddle on 15/3/9.
 */
public class FiveEight implements Captch {
	public OcrParse ocrParse;

	public void delaCaptch(String url, int trytimes) {
		PageGetter pageGetter = PageGetter.getPageGetterInstance();
		PageResult page = pageGetter.getPageContent(url);
		Html pagehtml = new Html(page.getHtmlContent());
		String uuid = pagehtml.xpath("//input[@id=\"uuid\"]/@value").toString();
		String imagerequest = "http://" + UrlFunction.getHostFromUrl(url)
				+ UrlFunction.getClearUrlFromUrl(url).replaceAll("valid", "code").replace(".do", "") + "/" + uuid + ".do";
		Map.Entry<Integer, String> answers = ocrParse.parseUrls(imagerequest, "1");
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		pairs.add(new BasicNameValuePair("namespace", "58.com"));
		pairs.add(new BasicNameValuePair("uuid", uuid));
		pairs.add(new BasicNameValuePair("inputcode", answers.getValue()));
		if (answers.getValue() == null) {
			return;
		}
		PageResult captchResult = pageGetter.getPagePostContent(
				"http://" + UrlFunction.getHostFromUrl(url) + UrlFunction.getClearUrlFromUrl(url), pairs);
		if (captchResult.getHtmlContent().equals("0")) {
		} else if (captchResult.getHtmlContent().equals("1")) {
			return;
		}
	}

	/**
	 * initCaptch的方法必须含有OcrObject
	 * <table>
	 * <td>Name</td>
	 * <td>Type</td>
	 * <td>意义</td>
	 * <tr>
	 * <td>OcrObject
	 * <td/>
	 * <td>OcrParse</td>
	 * <td>Ocr解析</td>
	 * <tr>
	 * </table>
	 * 
	 * @param maps
	 */

	@Override
	public void initCaptch(Map<String, Object> maps) {
		OcrParse parse = (OcrParse) maps.get("OcrObject");
		Assert.assertNotNullException(parse);
		this.ocrParse = parse;
	}

	/**
	 * @param url
	 * @param header
	 * @param cookie
	 * @return
	 */

	@Override
	public Map<String, Object> pareseCaptch(String url, Header header, Cookie cookie) {
		PageGetter pageGetter = PageGetter.getPageGetterInstance(UrlFunction.getHostFromUrl(url));
		PageResult page = pageGetter.getPageContent(url);
		Html pagehtml = new Html(page.getHtmlContent());
		String uuid = pagehtml.xpath("//input[@id=\"uuid\"]/@value").toString();
		String imagerequest = "http://" + UrlFunction.getHostFromUrl(url)
				+ UrlFunction.getClearUrlFromUrl(url).replaceAll("valid", "code").replace(".do", "") + "/" + uuid + ".do";
		Map.Entry<Integer, String> answers = ocrParse.parseUrls(imagerequest, UUConstant.RANDOM_LENGTH_TYPE);
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		pairs.add(new BasicNameValuePair("namespace", "58.com"));
		pairs.add(new BasicNameValuePair("uuid", uuid));
		pairs.add(new BasicNameValuePair("inputcode", answers.getValue()));
		if (answers.getValue() == null) {
			return null;
		}
		PageResult captchResult = pageGetter.getPagePostContent(
				"http://" + UrlFunction.getHostFromUrl(url) + UrlFunction.getClearUrlFromUrl(url), pairs);
		if (captchResult.getHtmlContent().equals("0")) {
		} else if (captchResult.getHtmlContent().equals("1")) {
			return null;
		}
		return null;
	}
}
